import axios from "axios";
import cheerio from "cheerio";
import Article from "../model/Article";

async function getAllUrl(url: string) {
  const { data: html } = await axios.get(url);
  const $ = cheerio.load(html);
  const [...data] = $(".blog-post>div>div:nth-of-type(1)>a");
  const path = data.map((item) => item.attribs.href);
  const result = await getData(path);
  await Article.bulkCreate(result);
  console.log("ok");
}
let i: number = 0;
let id: any = setInterval(() => {
  i++;
  getAllUrl(`https://www.ailoli.org/page/${i}/`);
  if (i >= 6) return clearInterval(id);
}, 3000);

getData(["https://www.ailoli.org/archives/123/"]);
async function getData(url: Array<string>) {
  const result = url.map(async (item) => {
    const { data: html } = await axios.get(item);
    const $ = cheerio.load(html);
    const content = $("#postpage").html()?.replace(/[\s]/g, "");
    let contentlenth = content?.length;
    const titles =
      $("#postpage").text().replace(/[ \n]/g, "").slice(0, 100) + "...";
    const title = $("#small_widgets>h1").text().trim();
    const author = $("#small_widgets>ul>li:nth-of-type(1)>a").text().trim();
    let time: string = $("#small_widgets>ul>li:nth-of-type(2)>time")
      .text()
      .trim();

    let str1: string = time.replace(/[ ]/g, "");
    let str2: string = str1.slice(str1.indexOf(",") + 1);
    const createdTime: number =
      parseInt(str2 + str1.slice(str1.indexOf(",") - 1, str1.indexOf(","))) - 1;
    const browesNum = parseInt(
      $("#small_widgets>ul>li:nth-of-type(3)>span:nth-of-type(2)").text().trim()
    );
    let comment = parseInt(
      $("#small_widgets>ul>li:nth-of-type(4)>a").text().trim()
    );
    comment = comment ? comment : 0;
    const fontlenth = parseInt(
      $("#small_widgets>ul>li:nth-of-type(5)>span:nth-of-type(2)").text().trim()
    );
    return {
      content,
      title,
      author,
      createdTime,
      browesNum,
      comment,
      fontlenth,
      titles,
      contentlenth,
    };
  });
  const data = await Promise.all(result);
  return data;
}
